import $H from '@/common/free-lib/request.js';
export default {
	getPrepayID(order, order_url) {
		uni.showLoading({
			title: '正在启动支付....'
		})
		var that = this;
		var platform = uni.getStorageSync('platform');
		if (platform == 'WEIXIN') { //微信小程序
			return this.wx_mch_pay_code(order, order_url);
		} else if (platform == 'H5') { //H5网页
			var is_mp = uni.getStorageSync('is_mp');
			if (is_mp == 2) { //微信公众号
				return this.wx_mp_pay_code(order, order_url);
			} else { //h5
				return this.wx_h5(order, order_url);
			}
		}
	},
	//h5微信网页支付
	// #ifdef H5
	wx_h5(order, order_url) {
		return new Promise((res_pay, rej) => {
			uni.showToast({
				title: '付款成功',
				icon: 'none'
			})
			res_pay(200)
		});
	},
	//微信公众号
	wx_mp_pay_code(order, order_url) {
		return new Promise((res_pay, rej) => {
			let requestParams = {
				user_id: uni.getStorageSync('user_id'),
				order: order
			};
			$H.post(order_url, 'app', {
				token: false,
				datalist: requestParams
			}).then(ret => {
				uni.hideLoading()
				if (ret.data.status == 200) {
					wxJS.config({
						// 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
						debug: false,
						// 必填，公众号的唯一标识
						appId: ret.data.data.g_appid,
						// 必填，生成签名的时间戳
						timestamp: ret.data.data.timeStamp,
						// 必填，生成签名的随机串
						nonceStr: ret.data.data.nonceStr,
						// 必填，签名
						signature: '',
						// 必填，需要使用的JS接口列表
						jsApiList: []

					});
					wxJS.chooseWXPay({
						timestamp: ret.data.data.timeStamp,
						nonceStr: ret.data.data.nonceStr,
						package: ret.data.data.package,
						signType: ret.data.data.signType,
						paySign: ret.data.data.paySign,
						success: (res) => {
							if (res.errMsg === 'chooseWXPay:ok') {
								//支付成功
								uni.showToast({
									title: '付款成功',
									icon: 'none'
								})
								res_pay(200);

								return

							} else {
								uni.showToast({
									title: '支付失败',
									icon: 'none'
								})
								res_pay(0);
								return;

							}
						},
						fail: (e) => {
							uni.showToast({
								title: '支付失败',
								icon: 'none'
							})
							res_pay(0);
							return;
						}
					});



					return
				}
				uni.showToast({
					title: ret.data.msg,
					icon: 'none'
				})
				res_pay(0);
			})
		});

	},
	// #endif
	//微信小程序支付
	wx_mch_pay_code(order, order_url) {
		return new Promise((res_pay, rej) => {
			uni.login({
				provider: 'weixin',
				success: function(loginRes) {
					//loginRes中有code，拿着code再请求自己服务器以获取openid  
					console.log("order: " + JSON.stringify(order));
					let requestParams = {
						user_id: uni.getStorageSync('user_id'),
						code: loginRes.code,
						order: order
					};
					$H.post(order_url, 'app', {
						token: false,
						datalist: requestParams
					}).then(ret => {
						uni.hideLoading()
						if (ret.data.status == 200) {
							uni.requestPayment({
								nonceStr: ret.data.data.nonceStr,
								package: ret.data.data.package,
								paySign: ret.data.data.paySign,
								timeStamp: ret.data.data.timeStamp,
								signType: 'MD5',
								success(res) {
									//支付成功 
									uni.showToast({
										title: '付款成功',
										icon: 'none'
									})
									res_pay(200);

									return
								},
								fail: function(err) {
									uni.showToast({
										title: '支付失败',
										icon: 'none'
									})
									res_pay(0);
									return;
								}
							})

							return
						}
						uni.showToast({
							title: ret.data.msg,
							icon: 'none'
						})
						res_pay(0);
					})

				}
			});
		});

	},
	//微信小程序支付
	wx_mch_pay_data(data) {
		return new Promise((res_pay, rej) => {
			uni.requestPayment({
				nonceStr: data.nonceStr,
				package: data.package,
				paySign: data.paySign,
				timeStamp: data.timeStamp,
				signType: 'MD5',
				success(res) {
					//支付成功 
					uni.showToast({
						title: '付款成功',
						icon: 'success'
					})
					res_pay(200);
					return
				},
				fail: function(err) {
					uni.showToast({
						title: '支付失败',
						icon: 'error'
					})
					res_pay(0);
					return;
				}
			})
		});

	},
	//app微信支付
	app_wx_mch_pay_data(data) {
		return new Promise((res_pay, rej) => {

			uni.requestPayment({
				"provider": "wxpay",
				"orderInfo": data,
				success(res) {
					//支付成功
					uni.hideLoading()
					uni.showToast({
						title: '付款成功',
						icon: 'success'
					})
					res_pay(200);
					return
				},
				fail(e) {
					uni.hideLoading()
					uni.showToast({
						title: '支付失败',
						icon: 'error'
					})
					res_pay(0);
					return;
				}
			})

		});

	},

}